package com.rabbiter.oes.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.oes.entity.Admin;
import com.rabbiter.oes.mapper.AdminMapper;
import com.rabbiter.oes.service.AdminService;
import com.rabbiter.oes.utils.PasswordEncryptionExample;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public List<Admin> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public Admin findById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int deleteById(int id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int update(Admin admin) {
        return baseMapper.updateById(admin);
    }

    @Override
    public int add(Admin admin) {
        return baseMapper.insert(admin);
    }

    @Override
    public void resetPsw(Integer id, String newPsw, String oldPsw) {
        Assert.isTrue(ObjectUtil.isNotEmpty(id), "未获取到id");
        Assert.isTrue(ObjectUtil.isNotEmpty(newPsw), "新密码未获取到");
        Assert.isTrue(ObjectUtil.isNotEmpty(oldPsw), "确认密码未获取到");
        Admin admin = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotEmpty(admin), "管理员信息未获取到");
        Assert.isTrue(PasswordEncryptionExample.checkPassword(oldPsw, admin.getPwd()), "原密码错误");

        admin.setPwd(PasswordEncryptionExample.encrypted(newPsw));
        baseMapper.updateById(admin);
    }
}
